package cn.soft863.servlet.back;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.soft863.bike.pojo.ABike;
import cn.soft863.bike.pojo.ACate;
import cn.soft863.bike.service.IBikeService;
import cn.soft863.bike.service.impl.BikeServiceImpl;
import cn.soft863.common.servlet.BaseServlet;
import cn.soft863.common.util.DateUtil;
import cn.soft863.common.util.PageInfoUtil;
import cn.soft863.common.util.VerifyCodeUtil;
import cn.soft863.users.pojo.AAdmins;

public class BikeServlet extends BaseServlet
{
	private IBikeService bikeService = new BikeServiceImpl();

	private DateUtil dateUtil = new DateUtil();

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		String method = request.getParameter("method");
		if ("bikeInsert".equalsIgnoreCase(method))
		{
			this.bikeInsert(request, response);
		} else if ("bikeInsertSubmit".equalsIgnoreCase(method))
		{
			this.bikeInsertSubmit(request, response);
		} else if ("bikeList".equalsIgnoreCase(method))
		{
			this.bikeList(request, response);
		} else if ("bikeUpdate".equalsIgnoreCase(method))
		{
			/* 打开管理员的添加页面呢 */
			bikeUpdate(request, response);
		} else if ("bikeUpdateSubmit".equalsIgnoreCase(method))
		{
			/* 打开管理员的添加提交页面呢 */
			bikeUpdateSubmit(request, response);
		}

	}

	private void bikeList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		String currentPage = request.getParameter("currentPage");
		String pageSize = request.getParameter("pageSize");

		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		/*
		 * currentPage如何木有传或者传入的不是整数,咋办? 转换数据会报错,咋办?
		 */
		try
		{
			pageInfoUtil.setCurrentPage(Integer.valueOf(currentPage));
			pageInfoUtil.setPageSize(Integer.valueOf(pageSize));
		} catch (NumberFormatException e)
		{
		}

		/*
		 * 传搜索要用的
		 * 
		 * 凡是Servlet需要的东西,需要的参数,都问jsp要,都从request里面取
		 */
		String keyword = request.getParameter("keyword");
		String status = request.getParameter("status");
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");

		if (keyword == null)
		{
			keyword = "";
		}
		/* 取掉前后空格 */
		keyword = keyword.trim();
		if (status == null)
		{
			status = "";
		}
		if (st == null)
		{
			st = "";
		}
		if (ed == null)
		{
			ed = "";
		}

		Date stDate = null;
		Date edDate = null;

		/* 如何将字符串的日期时间变成Date对象呢? */
		if (!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			stDate = dateUtil.strToDateTime(st);
			edDate = dateUtil.strToDateTime(ed);
		}

		Map<String, Object> condMap = new HashMap<String, Object>();
		/* 按照关键字查询 */
		condMap.put("keyword", keyword);
		/* 按照状态查询 */
		condMap.put("status", status);
		condMap.put("stDate", stDate);
		condMap.put("edDate", edDate);

		/**
		 * 终于将查询的结果放到了集合中
		 */
		List<ABike> bikeList = this.bikeService.findCondListBikeService(pageInfoUtil, condMap);
		/* 将Service调用方法的返回值放到四大作用域中. */
		request.setAttribute("bikeList", bikeList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);

		/* 存储搜索条件 */
		request.setAttribute("keyword", keyword);
		request.setAttribute("status", status);
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);

		this.url = "/WEB-INF/jsp/back/bike/bikeList.jsp";
		/*
		 * 服务器端跳转到jsp上面. 所有的jsp都放到web-inf/jsp/back中
		 */
		request.getRequestDispatcher(url).forward(request, response);

	}

	private void bikeUpdateSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		System.out.println("=======bikeUpdateSubmit====");
		/* 接收参数
		 * 不管是post还是get,提交的字符串都是以此种格式显示
		 * 键=值&键=值&键=值&键=值&键=值&键=值&键=值&
		 * 参数是键
		 * method=bikeUpdate&aid=4
		 * 
		 * 获取Id */
		String id = request.getParameter("aid");
		HttpSession session = request.getSession();
		AAdmins adminsSess = (AAdmins) session.getAttribute("admins");
		
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		ABike bike = this.bikeService.findOneBikeService(condMap);

		/* 接收参数 */
		
		String cateId = request.getParameter("cateId");
		String name = request.getParameter("name");
		String price = request.getParameter("price");
		
		String status = request.getParameter("status");
		
		/* 处理参数 */
		int cateIdInt = Integer.parseInt(cateId) ;
		double priceDouble = Double.parseDouble(price);
		byte statusByte = Byte.valueOf(status);
		Random ram = new Random();
		String rand = "";
		for (int j = 1; j <= 4; j++)
		{
			int i = ram.nextInt(9);
			rand += i;
		}
		/* 赋值 */	
		/* 页面上不需要赋值的,后台直接填写 */
		

		bike.setCreateId(adminsSess.getId());
		bike.setUpdateId(adminsSess.getId());
		bike.setCateId(cateIdInt);
		bike.setName(name);
		bike.setPassword(rand);
		bike.setPrice(priceDouble);
		bike.setStatus(statusByte);
		bike.setUpdateTime(new Date());
		
		/* 调用Service的方法,存储结果
		 * Test类,真的就可以将数据存储到数据库了
		 *  */
		int res = this.bikeService.updateOneBikeService(bike);
		System.out.println(res + "------------------");
		/* 存储结果 */
		if(res > 0 )
		{
			/* Sucess */
			this.info = "成功,id:" + bike.getId() ; 
		}else
		{
			this.info = "失败" ;
		}
		request.setAttribute("info", info);
		
		
		this.url = "/WEB-INF/jsp/back/bike/bikeUpdate.jsp" ; 
		request.getRequestDispatcher(url).forward(request, response);

	}

	private void bikeUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		System.out.println("=======bikeUpdate====");

		/*
		 * 接收参数 不管是post还是get,提交的字符串都是以此种格式显示 键=值&键=值&键=值&键=值&键=值&键=值&键=值& 参数是键
		 * method=bikeUpdate&aid=4
		 * 
		 * 获取Id
		 */
		String id = request.getParameter("aid");

		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", id);
		ABike bike = this.bikeService.findOneBikeService(condMap);
		/* 将结果存储到四大作用域中 */
		request.setAttribute("bike", bike);

			/* 只查询启用的 */
			condMap.put("status", "1");
			List<ACate> cateList = this.bikeService.findCondListCateService(null, condMap);
			/* 放到四大作用域 */
			request.setAttribute("cateList", cateList);

		url = "/WEB-INF/jsp/back/bike/bikeUpdate.jsp";
		request.getRequestDispatcher(url).forward(request, response);

	}

	private void bikeInsertSubmit(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		System.out.println("====bikeInsertSubmit========");
		HttpSession session = request.getSession();
		AAdmins adminsSess = (AAdmins) session.getAttribute("admins");

		/* 接收参数 */
		String cateId = request.getParameter("cateId");
		String name = request.getParameter("name");
		String price = request.getParameter("price");
		String status = request.getParameter("status");

		/* 处理参数 */
		double priceDou = Double.valueOf(price);
		byte statusByte = Byte.valueOf(status);

		//获取分类
		Map<String, Object> condMap = new HashMap<String, Object>();

		/* 只查询启用的 */
		condMap.put("status", "1");
		List<ACate> cateList = this.bikeService.findCondListCateService(null, condMap);
		/* 放到四大作用域 */
		request.setAttribute("cateList", cateList);
		/* new对象 */
		ABike bike = new ABike();

		/* jsp页面上的参数 */
		bike.setCateId(Integer.valueOf(cateId));
		bike.setName(name);
		bike.setPrice(priceDou);
		bike.setStatus(statusByte);

		// 创建随机密码
		Random ram = new Random();
		String rand = "";
		for (int j = 1; j <= 4; j++)
		{
			int i = ram.nextInt(9);
			rand += i;
		}

		bike.setPassword(rand);

		/* 管理员 */
		bike.setCreateId(adminsSess.getId());
		bike.setUpdateId(adminsSess.getId());

		/* 系统默认填写的值 */
		bike.setCreateTime(new Date());
		bike.setUpdateTime(new Date());
		bike.setPubTime(new Date());

		/* 调用Service保存 */
		int res = this.bikeService.saveOneBikeService(bike);
		if (res > 0)
		{
			this.info = "添加成功";
		} else
		{
			this.info = "添加失败";
		}

		/* 存储结果 */
		request.setAttribute("info", info);
		this.bikeInsert(request, response);

	}

	private void bikeInsert(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException
	{
		System.out.println("====bikeInsert========");
		/*
		 * 查询分类表中所有启用的信息 如果查询的数据不能分页,这些得很少才行
		 */
		Map<String, Object> condMap = new HashMap<String, Object>();

		/* 只查询启用的 */
		condMap.put("status", "1");
		List<ACate> cateList = this.bikeService.findCondListCateService(null, condMap);
		/* 放到四大作用域 */
		request.setAttribute("cateList", cateList);

		/* 服务器端跳转到jsp */
		this.url = "/WEB-INF/jsp/back/bike/bikeInsert.jsp";
		request.getRequestDispatcher(url).forward(request, response);
	}

}
